from rest_framework.response import Response
from rest_framework.pagination import LimitOffsetPagination, PageNumberPagination
from collections import OrderedDict   # OrderedDict有序字典

def jwt_response_payload_handler(token, user=None, request=None):
    # 返回响应重写
    return {
        'token': token,
        'id': user.id,
        'username': user.username
    }



class PageNum(PageNumberPagination):
    # 默认每页几条记录
    page_size = 10
    # 前端要求的数据格式
    page_size_query_param = 'pagesize'
    # 最大数量
    max_page_size = 50

    def get_paginated_response(self, data):
        return Response(OrderedDict([
            ('count', self.page.paginator.count),     # 一共多少条数据
            ('lists', data),                          # 结果显示
            ('page', self.page.number),               # 当前页数
            ('pages', self.page.paginator.num_pages), # 一共多少页
            ('pagesize', self.page_size)              # 一页多少条数据
        ]))

